package com.example.beikeyigou.setting;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.beikeyigou.R;
import com.example.beikeyigou.login_reg.UserBean;
import com.example.beikeyigou.utils.BaseActivity;
import com.example.beikeyigou.utils.SaveUserUtils;
import com.example.beikeyigou.utils.URLUtils;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

public class NewPwd2Activity extends BaseActivity {
    UserBean.DataBean userData, newUserData;

    private EditText newPwdEt, rePwdEt;
    private Button confirmBtn;
    private ImageView backImg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_pwd2);

        findView();
        refreshData();
        setListener();
        changeStatusBarTextImgColor(true);
    }

    public void changeStatusBarTextImgColor(boolean color) {
        if (color) {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);//实现状态栏图标和文字颜色为暗色
        } else {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);//恢复状态栏白色字体
        }
    }

    private void setListener() {
        confirmBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String newPwd = newPwdEt.getText().toString();
                String rePwd = rePwdEt.getText().toString();
                if (judgeInput(newPwd, rePwd)) {
                    newUserData.setPassword(newPwd);
                    Map<String, String> map = new HashMap<>();
                    map.put("userId", String.valueOf(userData.getId()));
                    map.put("oldPwd", userData.getPassword());
                    map.put("newPwd", newPwd);
                    postNetwork(URLUtils.updatePwd_url, map);
                }
            }
        });

        backImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private boolean judgeInput(String newPwd, String rePwd) {
        if (TextUtils.isEmpty(newPwd)||TextUtils.isEmpty(rePwd)) {
            Toast.makeText(this, "输入内容不能为空！", Toast.LENGTH_SHORT).show();
            return false;
        };
        // 两次输入的新密码是否一致
        if (!newPwd.equals(rePwd)) {
            Toast.makeText(this, "两次输入的密码不一致！", Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }

    @Override
    public void onSuccess(String result) {
        super.onSuccess(result);
        // 解析数据
        UserBean bean = new Gson().fromJson(result, UserBean.class);
        int code = bean.getCode();
        switch (code) {
            case 200:       // 修改成功
                Toast.makeText(this, "修改成功！", Toast.LENGTH_SHORT).show();
                // 保存修改信息
                SaveUserUtils.saveUserToFile(this, newUserData);

                Intent intent = new Intent(NewPwd2Activity.this, SettingActivity.class);
                // Intent.FLAG_ACTIVITY_CLEAR_TOP：销毁目标Activity和它之上的所有Activity，重新创建目标Activity
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                break;
            case 201:
                Toast.makeText(this, "旧密码错误！", Toast.LENGTH_SHORT).show();
                break;
            default:        // 其他，修改失败
                Toast.makeText(this, "修改失败", Toast.LENGTH_SHORT).show();
                break;
        }
    }

    private void refreshData() {
        userData = SaveUserUtils.getUserData(this);
        newUserData = new UserBean.DataBean(userData.getId(), userData.getPhone(),
                userData.getPassword(), userData.getName(), userData.getAddress(),
                userData.getNickname(), userData.getQuestion(), userData.getAnswer());
        if (userData == null) {
            Toast.makeText(this, "请先登录", Toast.LENGTH_SHORT).show();
            finish();
        }
    }

    private void findView() {
        newPwdEt = findViewById(R.id.pwdChange2_newPwd);
        rePwdEt = findViewById(R.id.pwdChange2_reNewPwd);
        confirmBtn = findViewById(R.id.pwdChange2_btn);
        backImg = findViewById(R.id.pwdChange2_back);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if(getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT){
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
    }
}